package com.menu;

import java.util.ArrayList;
import java.util.List;

public class Menu {
	
	private List<MenuItem> items = new ArrayList<MenuItem>();
	
	// 默认显示2层菜单
	private int showLevel = 2; 
	
	public Menu() {
	}
	
	public MenuItem[] getItems() {
		return this.items.toArray(new MenuItem[this.items.size()]);
	}
	
	public Menu addItem(MenuItem item) {
		if(item == null) {
			return this;
		}
		item.setIndex(this.items.size() + "");
		item.setPindex("-1");
		
		this.items.add(item);
		
		return this;
	}
	
	public String doView() {
		StringBuffer html = new StringBuffer();
		html.append("<ul>");
		for(MenuItem item : items) {
			doView(html, item);
		}
		html.append("</ul>");
		return html.toString();
	}
	
	/**
	 * 显示顶部主菜单
	 * @param html
	 * @param item
	 */
	private void doView(StringBuffer html, MenuItem item) {
		if(item == null || !item.isShow()) {
			return;
		}
		
		boolean hasChild = (item.getChildren().length > 0);
		
		MenuItem childItem = null;
		if(hasChild) {
			childItem = item.getChildren()[0];
		}
		
		html.append("<li>");
		html.append("<a ")
					.append("href='#' ")
					.append("src='" + item.getHref() + "' ")
					.append("target='" + item.getTarget() + "' ")
					.append("level='" + item.getLevel() + "' ")
					.append("index='" + item.getIndex() + "' ")
					.append("pindex='" + item.getPindex() + "' ")
					.append("hasChild='" + hasChild + "' ")
					.append("position='" + item.getPosition() + "' ")
					.append("type='" + item.getType() + "' ")
					.append("isSelected='" + item.isSelected() +"' ");
		if(childItem != null) {
				html.append("childSrc='" + childItem.getHref() + "' ");
		}
				html.append("onclick='clickMenuItem();' ")
					.append(">")
					.append(item.getText())
			.append("</a>");
		
		if(hasChild && item.getLevel() < this.getShowLevel() && 
				item.getPosition() == 0) {
			html.append("<ul>");
			for(MenuItem child : item.getChildren()) {
				child.setLevel(item.getLevel() + 1);
				doView(html, child);
			}
			html.append("</ul>");
		}
		
		html.append("</li>");
	}
	
	public MenuItem getItemByIndex(String index) {
		MenuItem item = null;
		
		if(index == null && "".equals(index)) {
			return item;
		}
		
		int idx = Integer.valueOf(index);
		
		item = this.items.get(idx);
		
		return item;
	}
	
	public static void main(String[] args) {
		Menu m = new Menu();
		MenuItem item = new MenuItem("首页1", "baidu");
		m.addItem(item);
		
		item = new MenuItem("首页2", "g.cn");
		
		MenuItem subItem = new MenuItem("首页2.1", "g.cn1");
		item.addChild(subItem);
		
		subItem = new MenuItem("首页2.2", "g.cn2");
		item.addChild(subItem);
		
		m.addItem(item);
		
		item = new MenuItem("首页3", "sina");
		m.addItem(item);
		
		
		String html = m.doView();
		
		System.out.println(html);
	}

	public int getShowLevel() {
		return showLevel;
	}

	public void setShowLevel(int showLevel) {
		this.showLevel = showLevel;
	}
}
